<template>
  <div class="cfDetailsbox">
    <!-- 顶部告白内容 -->
    <el-card style="margin-bottom: 5px; width: 100%">
      <!-- <div>{{ $route.params.confessionDetailData }}</div> -->
      <div class="post">
        <div class="user-block">
          <img
            class="img-circle"
            :src="avatarImages[$route.params.confessionDetailData.avatar] + avatarPrefix"
            alt=""
          >
          <span class="username text-muted">{{ $route.params.confessionDetailData.nickName }}</span>
          <span class="description">{{ $route.params.confessionDetailData.createDate }}</span>
          <!-- <span class="description">{{ formatTime(new Date($route.params.confessionDetailData.createDate)) }}</span> -->
        </div>
        <p class="confession-content">
          <span>{{ $route.params.confessionDetailData.lover }}</span>
          <img src="../../../assets/icons/horn2.png" class="like-icon" alt="">
          {{ $route.params.confessionDetailData.content }}
        </p>
        <ul class="list-inline">
          <li>
            <span class="link-black text-sm">
              <img src="../../../assets/icons/like.png" class="like-icon" alt="">{{ $route.params.confessionDetailData.likeCount }}点赞
            </span>
          </li>
          <li>
            <span class="link-black text-sm">
              <img
                src="../../../assets/icons/comment.png"
                class="like-icon"
                alt=""
              >{{ $route.params.confessionDetailData.commentCount }}条评论
            </span>
          </li>
        </ul>
      </div>
    </el-card>

    <!-- 发表评论 -->
    <div class="commitcontent">
      <el-input
        v-model="textarea"
        class="cominput"
        type="textarea"
        cols="70"
        rows="1"
        placeholder="请输入内容"
        maxlength="1024"
        clearable
        autosize
        show-word-limit
      />
      <el-button class="cmbtn" type="primary" @click="commitComments">提交♥</el-button>
    </div>

    <!-- 下部评论区域 -->
    <div class="commentArea">
      <p class="commentTitle">评论区</p>
      <div
        v-infinite-scroll="load"
        :infinite-scroll-disabled="scrolDisable"
        :infinite-scroll-distance="10"
        style="list-style-type: none; height:75vh; overflow-y: auto;"
      >
        <li v-for="(item, index) in commentData" :key="index" class="infinite-list-item">
          <el-card class="listrow">
            <div class="cardrow">
              <div class="con">{{ item.content }}</div>
              <div class="timebox">
                <div class="time">{{ item.createTime }}</div>
              </div>
            </div>

          </el-card>
        </li>
      </div>
    </div>
  </div>
</template>

<script>
// import { listCommentResponse } from '../listComment'
import { fetchCommentList, createComment } from '@/api/comment'
import { formatTime } from '@/utils/index.js'
const avatarPrefix = '?imageView2/1/w/80/h/80'

export default {
  name: 'ConfessionDetail',
  props: {
    // 首页传入 告白首页传过来的告白详情数据
    confessionDetailData: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      avatarImages: [
        require('@/assets/avatar/1.png'),
        require('@/assets/avatar/2.png'),
        require('@/assets/avatar/3.png'),
        require('@/assets/avatar/4.png'),
        require('@/assets/avatar/5.png'),
        require('@/assets/avatar/6.png'),
        require('@/assets/avatar/7.png')
      ],
      avatarPrefix,
      // 评论列表数据
      commentData: [],
      commentQuery: { mid: 0, page: 1, size: 10 },
      textarea: '',
      commentNum: 0,
      commentTotalNum: 0,
      commentNextPage: 0,
      scrolDisable: false
    }
  },
  watch: {
    commentNum(newVal) {
      if (newVal === this.commentTotalNum) {
        this.scrolDisable = true
      }
    }
  },
  mounted() {
    this.textarea = ''
    this.getCommentData()
  },
  destroyed() {
    this.commentData = []
    this.commentNextPage = 0
    this.commentNum = 0
    this.commentTotalNum = 0

    // 清空输入框数据
    this.textarea = ''
  },
  methods: {
    formatTime,
    fetchCommentList,
    createComment,
    commitComments() {
      console.log('提交评论内容！text = ', this.textarea)
      const midStr = this.$route.params.confessionDetailData.id
      const uidStr = this.$route.params.confessionDetailData.uid
      const contentStr = this.textarea
      const queryData = { mid: midStr, uid: uidStr, content: contentStr }
      this.createComment(queryData).then(function(response) {
        console.log('createComment', response)
      })
    },
    load() {
      console.log('scrolLoad')
      this.getCommentData()
    },
    getCommentData() {
      const that = this
      console.log('上个页面传入数据：', this.$route.params.confessionDetailData)
      // this.commentQuery.mid = this.$route.params.confessionDetailData.id
      // this.commentQuery.uid = this.$route.params.confessionDetailData.uid
      // this.commentQuery.page = this.commentNextPage
      this.fetchCommentList(this.commentQuery).then(function(response) {
        console.log('fetchCommentList', response)
        that.commentData = that.commentData.concat(response.data.items)
        // this.commentData = listCommentResponse.data;
        that.commentTotalNum = response.data.total
        that.commentNum = that.commentData.length
        that.commentNextPage = response.data.nextPage
        console.log('commentData = ', that.commentData)
        console.log('commentTotalNum = ', that.commentTotalNum)
      })
    },
    refreshAll() {
      this.commentData = []
      this.commentNextPage = 0
      this.commentNum = 0
      this.commentTotalNum = 0

      // 清空输入框数据
      this.textarea = ''

      // 重新加载评论列表数据
      this.getCommentData()
    }
  }
}
</script>

<style lang="scss" scoped>
.post {
  font-size: 14px;
  color: #666;

  .image {
    width: 100%;
    height: 100%;
  }

  .user-images {
    padding-top: 20px;
  }
}

.list-inline {
  padding-left: 0;
  margin-left: -5px;
  list-style: none;

  li {
    display: inline-block;
    padding-right: 5px;
    padding-left: 5px;
    font-size: 13px;
  }

  .link-black {
    &:hover,
    &:focus {
      color: #999;
    }
  }
}

.user-block {
  .username,
  .description {
    display: block;
    margin-left: 50px;
    padding: 2px 0;
  }

  .username {
    font-size: 14px;
    color: #000;
  }

  :after {
    clear: both;
  }

  .img-circle {
    border-radius: 50%;
    width: 35px;
    height: 35px;
    float: left;
  }

  span {
    font-weight: 500;
    font-size: 12px;
  }
}

.like-icon {
  width: 16px;
  margin-bottom: -2px;
}

.cfDetailsbox {
  display: flex;
  flex-direction: column;
  justify-content: center;
  // background-color: aquamarine;
  width: 100%;
  height: 100%;
  padding: 10px;

  .commentArea {
    // background-color: antiquewhite;
    margin-right: 20px;
    width: 100%;
    // margin-top: 20px;
    margin-bottom: 20px;
    text-align: left;

    .commentTitle {
      font-size: larger;
      font-weight: 500;
      margin-left: 10px;
    }

    .listrow{
    display: flex;
    flex-direction: column;
    margin-right: 20px;
    justify-content: flex-start;
    margin-top: 10px;
ß
    .con{
      color: black;
      font-size: medium;
      text-align: left;
      padding: 10px;
    }
    .timebox{
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      align-items: flex-end;
      margin-top: 8px;
      .time{
        color: darkgray;
        font-size: smaller;
        margin-right: 20px;
      }
    }
    }
  }

  .commitcontent{
  margin-top: 20px;
  margin-right: 20px;
  margin-bottom: 10px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;

  .cominput{
    display: inline-block;
    // width: 1000px;
    // height: 200px;
  }

  .cmbtn{
    display: inline-block;
    width: 120px;
    height: 40px;
    text-align: center;
    margin-left: 10px;
  }
  .cmbtn:active{
    background-color: gray;
  }
}
}

</style>
